using System;
using System.Web.UI;

namespace Orbitone.MeetingRooms.RoomFinder.UserControls
{
    public partial class CalendarItem : UserControl
    {
        public CalendarItemModel CalendarItemModel
        {
            get
            {
                var calendarItemModel = new CalendarItemModel();

                calendarItemModel.Id = EventId.Value;
                calendarItemModel.Mailbox = EventMailbox.Value;
                calendarItemModel.Subject = txtSubject.Text;
                if (ddlRooms.SelectedItem != null)
                    calendarItemModel.RoomEmail = ddlRooms.SelectedItem.Value;
                else
                {
                    /*int index = 0;
                    while (index < calendarItemModel.Rooms.Count &&
                           calendarItemModel.Rooms[index].Value != calendarItemModel.RoomEmail)
                    {
                        index++;
                    }
                    ddlRooms.DataSource = calendarItemModel.Rooms;
                    ddlRooms.DataBind();
                    ddlRooms.SelectedIndex = index; 
                    calendarItemModel.RoomEmail = ddlRooms.SelectedItem.Value;*/
                }
                calendarItemModel.Date = Convert.ToDateTime(txtDate.Text);
                calendarItemModel.StartTime = Convert.ToDateTime(txtDate.Text + " " + txtStartTime.Text);
                calendarItemModel.EndTime = Convert.ToDateTime(txtDate.Text + " " + txtEndTime.Text);
                calendarItemModel.UserId = UserId.Value;

                return calendarItemModel;
            }
            set
            {
                CalendarItemModel calendarItemModel = value;

                int index = 0;
                while (index < calendarItemModel.Rooms.Count &&
                       calendarItemModel.Rooms[index].Value != calendarItemModel.RoomEmail)
                {
                    index++;
                }

                EventId.Value = calendarItemModel.Id;
                EventMailbox.Value = calendarItemModel.Mailbox;
                txtSubject.Text = calendarItemModel.Subject;
                txtSubject.Enabled = calendarItemModel.SubjectEnabled;
                ddlRooms.DataSource = calendarItemModel.Rooms;
                ddlRooms.DataBind();
                ddlRooms.SelectedIndex = index;
                txtDate.Text = calendarItemModel.Date.ToString("dd/MM/yyyy");
                txtStartTime.Text = calendarItemModel.StartTime.ToString("HH:mm");
                txtEndTime.Text = calendarItemModel.EndTime.ToString("HH:mm");
                UserId.Value = calendarItemModel.UserId;

                rfvSubject.ValidationGroup = calendarItemModel.ValidationGroup;
                rfvDate.ValidationGroup = calendarItemModel.ValidationGroup;
                cvDate.ValidationGroup = calendarItemModel.ValidationGroup;
                rfvStartTime.ValidationGroup = calendarItemModel.ValidationGroup;
                revStartTime.ValidationGroup = calendarItemModel.ValidationGroup;
                rfvEndTime.ValidationGroup = calendarItemModel.ValidationGroup;
                revEndTime.ValidationGroup = calendarItemModel.ValidationGroup;
                btnRequest.ValidationGroup = calendarItemModel.ValidationGroup;

                datepicker.CssClass = calendarItemModel.ValidationGroup + "datepicker";
                txtDate.CssClass = calendarItemModel.ValidationGroup + "txtdate";

                lblTitle.Text = calendarItemModel.Title;
                btnRequest.Text = calendarItemModel.TitleButton;
            }
        }

        public event EventHandler CalendarDateChanged;
        public event EventHandler CalendarItemUpdated;

        protected void SwitchCalDay(object sender, EventArgs e)
        {
            //txtDate.Text = calSelect.SelectedDate.ToString("dd/MM/yyyy");

            //if (CalendarDateChanged != null)
            //    CalendarDateChanged(this, EventArgs.Empty);
        }

        protected void DoRequest(object sender, EventArgs e)
        {
            if (CalendarItemUpdated != null)
                CalendarItemUpdated(this, EventArgs.Empty);
        }
    }
}